package com.woniu.car.common;


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Resource
    private  SwaggerProperties sp;

    @Bean
    public Docket getDocket(){
        List<Parameter> ps = Arrays.asList(
                new ParameterBuilder().required(false)
                        .modelRef(new ModelRef("string"))
                        .parameterType("header")
                        .name("TOKEN")
                        .description("Token参数")
                        .build()
        );
        ApiInfo af = new ApiInfoBuilder().contact(new Contact(sp.getName(),sp.getHost(),sp.getEmail()))
                .description(sp.getDescription())
                .title(sp.getTitle())
                .version(sp.getVersion())
                .build();
        Docket docket = new Docket(DocumentationType.SWAGGER_2);
        return docket.apiInfo(af)
                .select()
                .apis(RequestHandlerSelectors.basePackage(sp.getBasePackage()))
                .paths(PathSelectors.any())
                .build()
                .globalOperationParameters(ps);
    }
}
